package com.cskaoyan.service.wx.impl;

import com.cskaoyan.bean.pojo.Collect;
import com.cskaoyan.bean.pojo.CollectExample;
import com.cskaoyan.bean.wx.collect.*;
import com.cskaoyan.mapper.CategoryMapper;
import com.cskaoyan.mapper.CollectMapper;
import com.cskaoyan.mapper.GoodsMapper;
import com.cskaoyan.service.wx.WxCollectService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class WxCollectServiceImpl implements WxCollectService {

    @Autowired
    CollectMapper collectMapper;
    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    CategoryMapper categoryMapper;



    @Override//wx/collect/list
    public CollectListVo collectList(CollectListBo collectListBo) {

        PageHelper.startPage(collectListBo.getPage(),collectListBo.getSize());
        List<CollectListBeanVo> collectListBeanVo = collectMapper.select(collectListBo.getType());
        PageInfo<CollectListBeanVo> pageInfo = new PageInfo<CollectListBeanVo>(collectListBeanVo);
        int pages = pageInfo.getPages();
        CollectListVo collectListVo = new CollectListVo();
        collectListVo.setTotalPages(pages);
        collectListVo.setCollectList(collectListBeanVo);
        return collectListVo;

       /* CollectListVo collectListVo = new CollectListVo();

        collectListVo.setTotalPages(1);

        List<CollectListBeanVo> collectListVo1 = collectMapper.select(collectListBo);
        collectListVo.setCollectList(collectListVo1);

        return collectListVo;*/
    }

    @Override//wx/collect/addordelete
    public String collectAddordelete(Integer type, Integer valueId, Integer userId) {
        CollectExample collectExample = new CollectExample();
        CollectExample.Criteria criteria = collectExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        if (type != null){
            criteria.andTypeEqualTo(type.byteValue());
        }
        if (valueId != null){
            criteria.andValueIdEqualTo(valueId);
        }
        List<Collect> collects = collectMapper.selectByExample(collectExample);
        if (collects.isEmpty()){
            collectMapper.insertByTypeAndValueId(type,valueId,userId);
            return "add";
        }else {
            int deleted = collectMapper.getDeleteByTypeAndValueId(type,valueId,userId);
            if (deleted == 0) {
                collectMapper.deleteByTypeAndValueId(type, valueId, userId);
                return "deleted";
            }
            if (deleted == 1) {
                collectMapper.addByTypeAndValueId(type, valueId, userId);
                return "add";
            }
        }

        return null;
    }





}
